﻿using System;
using System.Collections.Generic;
using System.Text;
using PostSharp.Aspects.Dependencies;

namespace Arcana.Contract.Method.Returns.All.Are.Greater
{
    /// <summary>
    /// Represents a contract that adheres to <b>Method returns an enumerable collection that all it's items should be greater than</b> semantic.
    /// </summary>
    /// <remarks>
    /// The method return value should have a return type that is an instance of
    /// <see cref="System.Collections.IEnumerable"/> other wise an instance of
    /// <see cref="System.ArgumentException"/> is throws in run-time.
    /// </remarks>
    [Serializable]
    [ProvideAspectRole(Roles.Contract)]
    [ProvideAspectRole(Roles.Method)]
    [ProvideAspectRole(Roles.ReturnValue)]
    [ProvideAspectRole(Roles.Conditional)]
    [AspectRoleDependency(AspectDependencyAction.Commute, Roles.Conditional)]
    public sealed class ThanAttribute : EnumerableComparingContractBase.UniversalQuantification
    {
        private object _value;

        /// <summary>
        /// Initializes a new instance of <b>Method.Returns.All.Are.Greater.ThanAttribute</b>.
        /// </summary>
        /// <remarks>
        /// The method return value should have a return type that is an instance of
        /// <see cref="System.Collections.IEnumerable"/> other wise an instance of
        /// <see cref="System.ArgumentException"/> is throws in run-time.
        /// </remarks>
        /// <param name="value">The value which all items contained in enumerable collection returned by method should be greater than.</param>
        /// <exception cref="System.ArgumentNullException"><i>value</i> is <b>null</b>.</exception>
        public ThanAttribute(object value)
        {
            if (value == null) throw new ArgumentNullException();
            _value = value;
        }

        /// <summary>
        /// Performs validation on an item which is stored in a method return value of type <see cref="System.Collections.IEnumerable"/>.
        /// </summary>
        /// <remarks>
        /// The method return value should have a return type that is an instance of
        /// <see cref="System.Collections.IEnumerable"/> other wise an instance of
        /// <see cref="System.ArgumentException"/> is throws in run-time.
        /// </remarks>
        /// <param name="item">The item in the method return value as a collection, to be validated.</param>
        /// <returns><b>true</b> if <i>item</i> is validated, otherwise <b>false</b>.</returns>
        protected override bool ValidateItem(object item)
        {
            return Comparer.Compare(item, _value) > 0;
        }
    }
}
